'use strict';

const db = uniCloud.database();
const collection = db.collection('food-categories');

exports.main = async (event, context) => {
  const { operation, categoryIds, updateData } = event.data || {};
  
  if (!operation) {
    return {
      code: 400,
      message: '缺少操作类型',
      data: null
    };
  }
  
  if (!categoryIds || !Array.isArray(categoryIds) || categoryIds.length === 0) {
    return {
      code: 400,
      message: '缺少分类ID列表',
      data: null
    };
  }
  
  try {
    let result;
    let successCount = 0;
    let failCount = 0;
    const failReasons = [];
    
    switch (operation) {
      case 'batch-delete':
        // 批量删除
        for (const categoryId of categoryIds) {
          try {
            const deleteResult = await collection.doc(categoryId).remove();
            if (deleteResult.deleted === 1) {
              successCount++;
            } else {
              failCount++;
              failReasons.push(`分类 ${categoryId} 删除失败`);
            }
          } catch (error) {
            failCount++;
            failReasons.push(`分类 ${categoryId} 删除失败: ${error.message}`);
          }
        }
        break;
        
      case 'batch-update-status':
        // 批量更新状态
        if (updateData.status === undefined) {
          return {
            code: 400,
            message: '缺少状态数据',
            data: null
          };
        }
        
        for (const categoryId of categoryIds) {
          try {
            const updateResult = await collection.doc(categoryId).update({
              status: updateData.status,
              update_date: Date.now()
            });
            if (updateResult.updated === 1) {
              successCount++;
            } else {
              failCount++;
              failReasons.push(`分类 ${categoryId} 更新失败`);
            }
          } catch (error) {
            failCount++;
            failReasons.push(`分类 ${categoryId} 更新失败: ${error.message}`);
          }
        }
        break;
        
      case 'batch-update-sort':
        // 批量更新排序
        if (updateData.sort === undefined) {
          return {
            code: 400,
            message: '缺少排序数据',
            data: null
          };
        }
        
        for (const categoryId of categoryIds) {
          try {
            const updateResult = await collection.doc(categoryId).update({
              sort: updateData.sort,
              update_date: Date.now()
            });
            if (updateResult.updated === 1) {
              successCount++;
            } else {
              failCount++;
              failReasons.push(`分类 ${categoryId} 更新失败`);
            }
          } catch (error) {
            failCount++;
            failReasons.push(`分类 ${categoryId} 更新失败: ${error.message}`);
          }
        }
        break;
        
      default:
        return {
          code: 400,
          message: '不支持的操作类型',
          data: null
        };
    }
    
    return {
      code: 200,
      message: '批量操作完成',
      data: {
        successCount,
        failCount,
        failReasons,
        total: categoryIds.length
      }
    };
  } catch (error) {
    return {
      code: 500,
      message: '批量操作失败',
      data: null
    };
  }
};